package com.zhoug.camera3.ui.ocr.overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;

import com.zhoug.camera3.R;
import com.zhoug.common3.view.OverlayView;

import androidx.annotation.Nullable;

/**
 * 身份证选取遮罩
 *
 * @Author HK-LJJ
 * @Date 2020/3/25
 * @Description
 */
public class IdCardOverlayView extends OverlayView {
    private static final String TAG = "IdCardOverlayView";
    private int face = FACE_FRONT;
    public static final int FACE_FRONT = 0;//头像
    public static final int FACE_BACK = 1;//国徽
    private Bitmap bitmap;
    private Rect src = new Rect();//要绘制的Bitmap对象的矩形区域
    private Rect dst = new Rect();//要将bitmap绘制在屏幕的什么地方
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public IdCardOverlayView(Context context) {
        this(context, null);
    }

    public IdCardOverlayView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IdCardOverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        setFace(FACE_FRONT);
//        setRatio(1.52f);
        setSure(OverlayView.SURE_WIDTH);
        setSureWeight(0.9f);
        setScaleEnabled(false);
        setTranslateEnabled(false);
        drawFrameRect=false;
        drawCornerLine=false;
    }


    @Override
    protected void drawImage(Canvas canvas) {
        super.drawImage(canvas);
        if (bitmap != null) {
            measureDstRect();
            canvas.drawBitmap(bitmap, src, dst, paint);
        } else {
            Log.e(TAG, "bitmap is null");
        }
    }

    /**
     * 计算图片画的位置
     *
     * @return
     */
    private void measureDstRect() {
        /* float width = frameRect.width();
        float height = frameRect.height();

       if (face == FACE_FRONT) {
            int left = (int) (frameRect.left + (frameRect.right - frameRect.left) * 0.65f);
            int top = (int) (+frameRect.top + (frameRect.bottom - frameRect.top) * 0.15f);
            int right = (int) (left + width * 0.25f);
            int bottom = (int) (top + height * 0.6f);
            dst.set(left, top, right, bottom);
        } else {
            int left = (int) (frameRect.left + (frameRect.right - frameRect.left) * 0.05f);
            int top = (int) (+frameRect.top + (frameRect.bottom - frameRect.top) * 0.1f);
            int right = (int) (left + width * 0.25f);
            int bottom = (int) (top + height * 0.4f);
            dst.set(left, top, right, bottom);
        }*/
        dst.set((int)frameRect.left, (int)frameRect.top, (int)frameRect.right, (int)frameRect.bottom);

    }

    public void setFace(int face) {
        this.face = face;
        Drawable drawable = null;
        if (face == FACE_FRONT) {
            drawable = getContext().getResources().getDrawable(R.drawable.camera_idcard_font);
        } else {
            drawable = getContext().getResources().getDrawable(R.drawable.camera_idcard_back);
        }
        if (drawable instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
            if (bitmap != null) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                src.set(0, 0, width, height);
                setRatio(width * 1.0f / height);
            }
        }
    }

    public int getFace() {
        return face;
    }

}
